<?php
/**
 * 测评题目
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-17
 * Time: 下午2:37
 */
namespace app\modules\weixin\controllers;

use app\libs\Method;
use app\libs\WeixinControl;
use app\modules\weixin\models\LowProduct;
use app\modules\weixin\models\LowProductChat;
use app\modules\weixin\models\LowProductClass;
use app\modules\weixin\models\LowProductClassRegistration;
use app\modules\weixin\models\LowProductClassYuyue;
use app\modules\weixin\models\MemberOpenid;
use yii;


class IndexController extends WeixinControl
{

    public $enableCsrfValidation = false;

    /**
     * @Title  [列表]
     * @Author Don
     * @Date   2021-05-11
     * @return [type]
     */
    public function actionIndex()
    {
        $data = LowProduct::find()->asArray()->one();
        $sign = LowProductClassRegistration::find()->where("uid=$this->uid AND productId={$data['id']}")->one();
        if($sign){
            if($sign->status == 1){
                $type = 1;//查看课表
                $id = $sign->id;
            }else{
                $type = 2;//去付款
                $id = $sign->id;
            }
        }else{
            $type=0;//立即购买
            $id = 0;
        }
        $data['descripition'] = Method::addDomainToImgSrc($data['descripition'],'https://www.thinkuprep.com');
        $data['teacher'] = Method::addDomainToImgSrc($data['teacher'],'https://www.thinkuprep.com');
        die(Method::jsonGenerate(1,['product' => $data,'type' => $type,'id' => $id]));
    }




    public function actionClass()
    {
        $productId = Yii::$app->request->post('productId',1);
        $date = Yii::$app->request->post('date','str');
        $class = LowProductClass::find()->select('id,date')->asArray()->where("productId=$productId AND date like '%$date%'")->orderBy("date ASC,startTime ASC")->groupBy('date')->all();
        foreach ($class as $k=>$v){
            $child = LowProductClass::find()->asArray()->where("productId=1 AND date='{$v['date']}'")->orderBy("date ASC,startTime ASC")->all();
            foreach ($child as $key=>$value){
                $date = $value['date'].' '.$value['endTime'];
                $endTime = strtotime($date);
                $date = $value['date'].' '.$value['startTime'];
                $startTime = strtotime($date);
                if($endTime<time()){
                    $child[$key]['type'] = 4;
                }if($startTime<=time() AND $endTime>=time()){
                    $child[$key]['type'] = 3;
                }else{
                    $sign = LowProductClassYuyue::find()->where("uid=$this->uid AND classId={$value['id']}")->one();
                    if($sign){
                        $child[$key]['type'] = 1;
                    }else{
                        $child[$key]['type'] = 2;
                    }
                }
            }
            $class[$k]['chile'] = $child;
        }
        $type = 0;
        $registration = LowProductClassRegistration::find()->where("uid=$this->uid AND productId=$productId AND status=1")->one();
        if($registration){
            $type = 1;
        }
        $week = date('w');
        $teacher = LowProductChat::find()->asArray()->where("FIND_IN_SET($week,week)")->one();
        die(Method::jsonGenerate(1,['class' => $class,'type' => $type,'teacher' => $teacher]));
    }



    public function actionYuyue(){
        if(!$this->uid){
            die(Method::jsonGenerate(99));
        }
        $id = Yii::$app->request->post('id');
        $sign = LowProductClassYuyue::find()->where("uid=$this->uid AND classId=$id")->one();
        if($sign){
            die(Method::jsonGenerate(1,null,'已经预约'));
        }
        $productClass = LowProductClass::findOne($id);
        $product = LowProduct::findOne($productClass->productId);
        $registration = LowProductClassRegistration::find()->where("uid=$this->uid AND productId=$product->id AND FIND_IN_SET('$productClass->section',section) AND status=1")->one();
        if(!$registration){
            die(Method::jsonGenerate(0,null,'还未购买此单项，请先购买产品'));
        }
        $sign = LowProductClassRegistration::findOne($registration->pid);
        if($sign->status == 0){
            die(Method::jsonGenerate(0,null,'服务还为开通，等待他人付款完成'));
        }
        $model = new LowProductClassYuyue();
        $model->classId = $id;
        $model->uid = $this->uid;
        $model->createTime = time();
        $model->save();
        $openInfo = MemberOpenid::find()->where("uid=$this->uid AND type=1")->one();
        $access_token = Method::token();
        $touser = $openInfo->openId; // 用户的 openid
        $template_id = 'NfTQtTaFxsXnhcRrvEJjGzE6DVnkxruvJE2Ie0rhfSQ'; // 模板 ID
        $data = array(
            'thing9' => array(
                'value' => $productClass->name,
                'color' => '#173177'
            ),
            'time22' => array(
                'value' => $productClass->date.' '.$productClass->startTime.'~'.$productClass->endTime,
                'color' => '#173177'
            )
        );
        $url = 'https://wx.thinkwithu.com'; // 点击消息跳转的链接
        $re = Method::sendTemplateMessage($access_token, $touser, $template_id, $data, $url);
        die(Method::jsonGenerate(1,null,'预约成功'));
    }
}